export default class Tools {
  //获取浏览器滚动的高度
  getScrollTop() {
    let scrollTop = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
      scrollTop = document.documentElement.scrollTop;
    } else if (document.body) {
      scrollTop = document.body.scrollTop;
    }

    return scrollTop;
  }

  //文档高度
  getDocumentTop() {
    let scrollTop = 0,
      bodyScrollTop = 0,
      documentScrollTop = 0;

    if (document.body) {
      bodyScrollTop = document.body.scrollTop;
    }

    if (document.documentElement) {
      documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop =
      bodyScrollTop - documentScrollTop > 0 ? bodyScrollTop : documentScrollTop;
    return scrollTop;
  }

  //可视窗口高度
  getWindowHeight() {
    let windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
      windowHeight = document.documentElement.clientHeight;
    } else {
      windowHeight = document.body.clientHeight;
    }
    return windowHeight;
  }

  //滚动条滚动高度
  getScrollHeight() {
    let scrollHeight = 0,
      bodyScrollHeight = 0,
      documentScrollHeight = 0;
    if (document.body) {
      bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
      documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight =
      bodyScrollHeight - documentScrollHeight > 0
        ? bodyScrollHeight
        : documentScrollHeight;

    return scrollHeight;
  }

  //检查一个对象中是否含有空值
  objHasNull(objData, ignore = []) {
    // 如果含有空值则返回true , 如果不含有空值则返回false
    // 只检查第一层
    for (var key in objData) {
      if (ignore.includes(key)) {
        // 忽略本次循环
        console.log("忽略本次循环", key);
        continue;
      }

      if (!objData[key] && objData[key] !== false && objData[key] !== 0) {
        console.log("找到了空值", objData[key]);
        return true;
      }
    }

    return false;
  }

  escape2Html(str) {
    var arrEntities = { lt: "<", gt: ">", nbsp: " ", amp: "&", quot: '"' };
    return str.replace(/&(lt|gt|nbsp|amp|quot);/gi, function(all, t) {
      return arrEntities[t];
    });
  }

  html2Escape(sHtml) {
    return sHtml.replace(/[<>&"]/g, function(c) {
      return { "<": "&lt;", ">": "&gt;", "&": "&amp;", '"': "&quot;" }[c];
    });
  }

  getType(obj) {
    //tostring会返回对应不同的标签的构造函数
    var toString = Object.prototype.toString;
    var map = {
      "[object Boolean]": "boolean",
      "[object Number]": "number",
      "[object String]": "string",
      "[object Function]": "function",
      "[object Array]": "array",
      "[object Date]": "date",
      "[object RegExp]": "regExp",
      "[object Undefined]": "undefined",
      "[object Null]": "null",
      "[object Object]": "object"
    };
    if (obj instanceof Element) {
      return "element";
    }
    return map[toString.call(obj)];
  }

  deepClone(data) {
    var type = this.getType(data);
    var obj;
    if (type === "array") {
      obj = [];
    } else if (type === "object") {
      obj = {};
    } else {
      //不再具有下一层次
      return data;
    }
    if (type === "array") {
      for (var i = 0, len = data.length; i < len; i++) {
        obj.push(this.deepClone(data[i]));
      }
    } else if (type === "object") {
      for (var key in data) {
        obj[key] = this.deepClone(data[key]);
      }
    }
    return obj;
  }
}
